#include "Camera.h"

namespace Horse
{
	Camera::Camera(CameraEnum cameraEnum, const QVector3D& eye, const QVector3D& target, const QVector3D& up, float left, float right, float bottom, float top, float nearPlane, float farPlane)
		: m_cameraEnum(cameraEnum)
		, m_eye(eye)
		, m_target(target)
		, m_up(up)
		, m_left(left)
		, m_right(right)
		, m_bottom(bottom)
		, m_top(top)
		, m_nearPlane(nearPlane)
		, m_farPlane(farPlane)
	{
		updateLookat();
		updateProject();
	}
	void Camera::updateLookat()
	{
		u_lookat.setToIdentity();
		u_lookat.lookAt(m_eye, m_target, m_up);
	}
	void Camera::updateProject()
	{
		u_project.setToIdentity();
		if (m_cameraEnum == ORTHO) 
		{
			u_project.ortho(m_left, m_right, m_bottom, m_top, m_nearPlane, m_farPlane);
		}
		else if(m_cameraEnum == FRUSTUM)
		{
			u_project.frustum(m_left, m_right, m_bottom, m_top, m_nearPlane, m_farPlane);
		}
	}
	void Camera::useCamera(QOpenGLShaderProgram* shaderProgram)
	{
		shaderProgram->setUniformValue("u_lookat", u_lookat);
		shaderProgram->setUniformValue("u_project", u_project);
	}
	Camera* Camera::ortho(const QVector3D& eye, const QVector3D& target, const QVector3D& up, float left, float right, float bottom, float top, float nearPlane, float farPlane)
	{
		return new Camera(ORTHO, eye, target, up, left, right, bottom, top, nearPlane, farPlane);
	}
	Camera* Camera::frustum(const QVector3D& eye, const QVector3D& target, const QVector3D& up, float left, float right, float bottom, float top, float nearPlane, float farPlane)
	{
		return new Camera(FRUSTUM, eye, target, up, left, right, bottom, top, nearPlane, farPlane);
	}
}